package cn.tedu.account.tcc;

import cn.tedu.account.entity.Account;
import cn.tedu.account.mapper.AccountMapper;


import cn.tedu.common.tcc.ResultHolder;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import java.math.BigDecimal;
@Component
public class AccountTccActionImpl implements AccountTccAction {
    @Autowired
    private AccountMapper accountMapper;

    //1.一阶段失败，没有标记，回滚不执行
    //2.一阶段成功，有成功标记，其他模块执行失败，可以执行回滚，回滚完成删除标记
    @Transactional
    @Override
    public boolean prepare(BusinessActionContext ctx, Long userId, BigDecimal money) {
        Account account = accountMapper.findByUserId(userId);
        //BigDecimal residue = account.getResidue();
        //if (Double.valueOf(residue.toString())<Double.valueOf(money.toString()))
        if (account.getResidue().compareTo(money)<0){
            throw new RuntimeException("余额不足,请充值");
        }
        accountMapper.updateResidueToFrozen(userId, money);
        /*第一阶段成功时，设置成功标记 Action类--事务id--“p”
         * 添加此标记做幂等性控制
         * */
        ResultHolder.setResult(AccountTccAction.class, ctx.getXid(), "p");
        return true;
    }
    @Transactional
    @Override
    /*加synchronized避免多个线程同时提交造成数据出错*/
    public synchronized boolean commit(BusinessActionContext ctx) {
        /*二阶段执行前先检查标记是否存在，若存在，执行第二步，若不存在，则不执行*/
        if (ResultHolder.getResult(AccountTccAction.class, ctx.getXid() ) == null) {
            return true;
        }
        Long userId = Long.valueOf(ctx.getActionContext("userId").toString());
        BigDecimal money = new BigDecimal(ctx.getActionContext("money").toString());
        accountMapper.updateFrozenToUsed(userId, money);
        /*二阶段完成后删除标记，防止重复执行此操作*/
        ResultHolder.removeResult(AccountTccAction.class, ctx.getXid());
        return true;
    }
    @Transactional
    @Override
    public synchronized boolean rollback(BusinessActionContext ctx) {
        /*rollback阶段执行前先检查标记是否存在，若存在，执行rollback，若不存在，则不执行*/
        //一阶段失败，没有标记，回滚不执行
        if (ResultHolder.getResult(AccountTccAction.class, ctx.getXid() ) == null) {
            return true;
        }
        Long userId = Long.valueOf(ctx.getActionContext("userId").toString());
        BigDecimal money = new BigDecimal(ctx.getActionContext("money").toString());
        accountMapper.updateFrozenToResidue(userId, money);
        /*rollback执行完成后删除标记，防止重复执行此操作*/
        ResultHolder.removeResult(AccountTccAction.class, ctx.getXid());
        return true;
    }
}
